<!--
 * @Description:div运动属性
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-05 22:51:05
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>div运动属性</title>
	<style>
		#div1 {
			width: 150px;
			height: 200px;
			background: green;
			position: absolute;
			left: -150px;
		}

		#div1 span {
			position: absolute;
			width: 20px;
			height: 60px;
			line-height: 20px;
			background: #e5e5e5;
			right: -20px;
			top: 70px;
		}
	</style>
	<script>
		window.onload = function () {
			let odiv = document.getElementById("div1")
			odiv.onmouseover = function () {
				startMove(0)
			}
			odiv.onmouseout = function () {
				startMove(-150)
			}
			var timer = null

			function startMove(itarget) {
				let odiv = document.getElementById("div1")
				clearInterval(timer)
				timer = setInterval(() => {
					var speed = 0
					if (odiv.offsetLeft > itarget) {
						speed = -10
					} else {
						speed = 10
					}
					if (odiv.offsetLeft == itarget) {
						clearInterval(timer)
					} else {
						odiv.style.left = odiv.offsetLeft + speed + 'px'
					}
				}, 20)
			}
		}
	</script>
</head>

<body>
	<div>
		运动的框架 关闭已有的定时器<br>
		把运动和停止的隔开（if/else)<br>
		左负右正
	</div>
	<div id="div1">
		<span>分享到</span>
	</div>
</body>

</html>